package com.smedi.ismedi.constructionproject.core.port.adapter.persistence.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smedi.ismedi.constructionproject.core.domain.aggregatemodel.BidSection;
import com.smedi.ismedi.constructionproject.core.domain.aggregatemodel.ProjectBasic;
import com.smedi.ismedi.constructionproject.core.domain.repository.BidSectionRepository;
import com.smedi.ismedi.constructionproject.core.port.adapter.persistence.model.BidSectionEntity;
import com.smedi.ismedi.constructionproject.core.port.adapter.persistence.model.ProjectAssignmentEntity;
import com.smedi.ismedi.constructionproject.core.port.adapter.persistence.mybatis.mapper.BidSectionMapper;
import com.smedi.ismedi.web.starter.util.DozerUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author: xulinjun
 * @Date: 2021-6-18 15:54
 */
@Slf4j
@Repository
@AllArgsConstructor
public class BidSectionRepositoryImpl extends ServiceImpl<BidSectionMapper, BidSectionEntity> implements BidSectionRepository, IService<BidSectionEntity> {
    private final DozerUtil dozerUtil;

    @Override
    public BidSection selectByBidSectionId(Long bidSectionId) {
        BidSectionEntity row = this.getBaseMapper().selectById(bidSectionId);
        if (row != null) {
            // 需要考虑软删除这块内容
            return dozerUtil.convertor(row, BidSection.class);
        } else {
            return null;
        }
    }

    @Override
    public BidSection doInsert(BidSection bidSection) {
        BidSectionEntity bidSectionEntity = dozerUtil.convertor(bidSection, BidSectionEntity.class);
        this.getBaseMapper().insert(bidSectionEntity);
        return dozerUtil.convertor(bidSectionEntity, BidSection.class);
    }

    @Override
    public BidSection doUpdate(BidSection bidSection) {
        BidSectionEntity bidSectionEntity = dozerUtil.convertor(bidSection, BidSectionEntity.class);
        this.getBaseMapper().updateById(bidSectionEntity);
        return dozerUtil.convertor(bidSectionEntity, BidSection.class);
    }

    @Override
    public void doDeleteByBidSectionId(Long bidSectionId) {
        this.getBaseMapper().deleteById(bidSectionId);
    }

    @Override
    public List<BidSection> selectByProjectId(Long projectId) {
        QueryWrapper<BidSectionEntity> wrapper = new QueryWrapper();
        wrapper.eq("project_id", projectId);
        List<BidSectionEntity> list = this.getBaseMapper().selectList(wrapper);

        return dozerUtil.convertor(list, BidSection.class);
    }

    @Override
    public void doDeleteByProjectId(Long projectId) {
        QueryWrapper<BidSectionEntity> wrapper = new QueryWrapper();
        wrapper.eq("project_id", projectId);

        this.getBaseMapper().delete(wrapper);
    }
}
